package tool.library.widget;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;

/**
 * Created by xzp on 15/9/10.
 */
public class BarGroup extends LinearLayout{
    private onCheckedChangeListener onCheckedChangeListener;
    private ChildCheckListener childCheckListener;
    private boolean protectFromChecked;
    private int checkedId;
    public BarGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
        setOrientation(HORIZONTAL);
        init();
    }
    private void init(){
        childCheckListener=new ChildCheckListener();
        checkedId=-1;
    }
    private class ChildCheckListener implements CompoundButton.OnCheckedChangeListener{
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if(protectFromChecked)
                return;
            protectFromChecked = true;
            if (checkedId != -1) {
                setCheckedStateForView(checkedId, false);
            }
            protectFromChecked = false;

            int id = buttonView.getId();
            setCheckedId(id);
        }
    }
    public void check(int id) {
        if (id != -1 && (id == checkedId)) {
            return;
        }

        if (checkedId != -1) {
            setCheckedStateForView(checkedId, false);
        }

        if (id != -1) {
            setCheckedStateForView(id, true);
        }
        setCheckedId(id);
    }

    public void setOnCheckedChangeListener(BarGroup.onCheckedChangeListener onCheckedChangeListener) {
        this.onCheckedChangeListener = onCheckedChangeListener;
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        if (checkedId != -1) {
            protectFromChecked = true;
            setCheckedStateForView(checkedId, true);
            protectFromChecked = false;
            setCheckedId(checkedId);
        }
    }


    private void setCheckedId(int id) {
        checkedId = id;
        if (onCheckedChangeListener != null) {
            onCheckedChangeListener.onCheckedChanged(this, checkedId);
        }
    }
    @Override
    public void addView(View child, int index, ViewGroup.LayoutParams params) {
        RadioButton button=findFirstButton(child);
        if(button!=null)
        {
            if(button.isChecked())
            {
                protectFromChecked=true;
                if(checkedId!=-1)
                    setCheckedStateForView(checkedId,false);
                protectFromChecked=false;
                setCheckedId(button.getId());
            }
            button.setOnCheckedChangeListener(childCheckListener);
        }
        super.addView(child, index, params);
    }
    private void setCheckedStateForView(int viewId, boolean checked) {
        View checkedView = findViewById(viewId);
        if (checkedView != null && checkedView instanceof RadioButton) {
            ((RadioButton) checkedView).setChecked(checked);
        }
    }
    private RadioButton findFirstButton(View view)
    {
        if(view instanceof RadioButton)
            return (RadioButton) view;
        else if(view instanceof ViewGroup)
        {
            ViewGroup group= (ViewGroup) view;
            for(int i=0;i<group.getChildCount();i++)
            {
                RadioButton button=findFirstButton(group.getChildAt(i));
                if(button!=null)
                    return button;
            }
        }
        return null;
    }
    public interface onCheckedChangeListener{
        void onCheckedChanged(BarGroup group,int checkId);
    }
}
